define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function ($, undefined, Backend, Table, Form, undefined) {
    //读取选中的条目
    $.jstree.core.prototype.get_all_checked = function (full) {
        var obj = this.get_selected(), i, j;
        for (i = 0, j = obj.length; i < j; i++) {
            obj = obj.concat(this.get_node(obj[i]).parents);
        }
        obj = $.grep(obj, function (v, i, a) {
            return v != '#';
        });
        obj = obj.filter(function (itm, i, a) {
            return i == a.indexOf(itm);
        });
        return full ? $.map(obj, $.proxy(function (i) {
            return this.get_node(i);
        }, this)) : obj;
    };
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'recruit/recruit/index',
                    edit_url: 'recruit/recruit/edit',
                    add_url: 'recruit/recruit/add',
                    del_url: 'recruit/recruit/del',
                    exam_url: 'recruit/recruit/exam',//笔试录入
                    interview_url: 'recruit/recruit/interview',//面试录入
                    post_url: 'recruit/post/index',//岗位管理
                    step_url: 'recruit/step/index',//流程管理
                    columns_url: 'recruit/recruit/columns'//表单筛选
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'recruit_id',
                sortName: 'recruit_id',
                searchFormVisible: false,
                searchFormTemplate: 'customformtpl',
                search: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'recruit_id', title: __('Recruit_id')},
                        {field: 'recruit_title', title: __('Recruit_title')},
                        {field: 'recruit_thumb', title: __('Recruit_thumb'),formatter: Table.api.formatter.image, operate: false},
                        //{field: 'recruit_desc', title: __('Recruit_desc')},
                        {field: 'step_id', title: __('Step_id')},
                        {field: 'recruit_attach_desc', title:'所需附件说明',visible:false},
                        {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'updatetime', title: __('Updatetime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'other', title: '信息完善', table: table,events: Table.api.events.operate, formatter: Table.api.formatter.other},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $(".search input").attr('placeholder','标题');//快速搜索
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        exam: function () {//自定义
        	Controller.api.bindevent();
        },
        interview: function () {//自定义
        	Controller.api.bindevent();
        },
        columns: function () {
        	var $ids=$("#recruit_id").val();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'recruit/recruit/columns/ids/'+$ids,
                    multi_url: 'recruit/recruit/columns_save',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'column_id',
                sortName: 'sort',
                sortOrder: 'asc',
                columns: [
                    [
                        {checkbox: true,field:'column_check'},
                        {field: 'column_id', title: 'ID',align: 'left',visible: false},
                        {field: 'column_name', title: '列名',align: 'left'},
                        {field: 'column_name_cn', title: '列名（中文）',align: 'left'},
                        {field: 'column_type', title: '类型',align: 'left'},
                        {field: 'column_options', title: '选项',align: 'left',visible: false},
                        {field: 'is_required', title: '必填',align: 'center'},
                        {field: 'column_required', title: '验证规则',align: 'center'},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
	            pagination: false,
	            search: false,
	            commonSearch: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"), null, null, function () {
                    if ($("#treeview").size() > 0) {
                        var r = $("#treeview").jstree("get_all_checked");
                        $("input[name='row[rules]']").val(r.join(','));
                    }
                    return true;
                });
                //渲染权限节点树
                //销毁已有的节点树
                $("#treeview").jstree("destroy");
                Controller.api.rendertree(nodeData);
                //全选和展开
                $(document).on("click", "#checkall", function () {
                    $("#treeview").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
                });
                $(document).on("click", "#expandall", function () {
                    $("#treeview").jstree($(this).prop("checked") ? "open_all" : "close_all");
                });
                $("select[name='row[pid]']").trigger("change");
            },
            rendertree: function (content) {
                $("#treeview")
                        .on('redraw.jstree', function (e) {
                            $(".layer-footer").attr("domrefresh", Math.random());
                        })
                        .jstree({
                            "themes": {"stripes": true},
                            "checkbox": {
                                "keep_selected_style": false,
                            },
                            "types": {
                                "root": {
                                    "icon": "fa fa-folder-open",
                                },
                                "menu": {
                                    "icon": "fa fa-folder-open",
                                },
                                "file": {
                                    "icon": "fa fa-file-o",
                                }
                            },
                            "plugins": ["checkbox", "types"],
                            "core": {
                                'check_callback': true,
                                "data": content
                            }
                        });
            }
        }
    };
    return Controller;
});